package nml.webmail;

import java.io.File;
import java.io.IOException;

import org.apache.commons.io.FileUtils;
import org.apache.commons.lang.StringEscapeUtils;
import org.apache.commons.lang.StringUtils;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.internal.seleniumemulation.KeyEvent;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.WebDriverWait;

public class hinduhome_com extends WebMail {

	public void send(String uid, String password, String to, String subject, String content) throws Exception {

		try {
			String url = "http://mail.hinduhome.com/email/scripts/loginuser.pl";
			dr.get(url);

			dr.findElement(By.name("loginName")).sendKeys(uid);
			dr.findElement(By.name("user_pwd")).sendKeys(password);

			dr.findElement(By.name("login")).click();

			try {
				dr.findElement(By.name("action")).click();
			} catch (Exception e) {

			}

			waitWebElement(By.cssSelector("a[title=Compose]")).click();

			waitWebElement(By.xpath("//*[@id=\"eon-page\"]/iframe"));

			dr.switchTo().frame(dr.findElement(By.xpath("//*[@id=\"eon-page\"]/iframe")).getAttribute("name"));

			
			content = StringUtils.remove(content, '\r');
			content = StringUtils.remove(content, '\n');

			content = StringEscapeUtils.escapeJavaScript(content);

			executeScript("  document.getElementById('composeEditorContent').value='" + content + "'   ");
			executeScript("  document.getElementById('send_to').value='" + to + "'   ");
			executeScript("  document.getElementById('send_subject').value='" + subject + "'   ");
			executeScript("   document.getElementById('theForm').submit()   ");

		} catch (Exception e) {
			e.printStackTrace();
			throw e;
		} finally {
			dr.quit();
		}
	}

	public static void main(String[] args) {
		try {
			String uid = "qq331798654";
			String password = "qqQ313@#1754";
			String to = "o21x@qq.com";
			String subject = "图片频道十分钟告警";
			String content = FileUtils.readFileToString(new File("a.txt"));

			new hinduhome_com().send(uid, password, to, subject, content);
			Thread.sleep(60000);
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

}
